<?php

namespace app\api\controller\meiriyiwen;

use app\api\controller\Base;
use app\api\controller\Utils;
use think\Db;

/**
 * 图片接口
 */
class Images extends Base
{

    public function get_img($page = 1, $post_id = '')
    {
        $params['page'] = 1;
        $params['type'] = 'refresh';
        if ($page > 1) {
            $params['post_id'] = $post_id;
            $params['type'] = 'loadmore';
        }
        $res = $this->http('https://api.tuchong.com/feed-app', $params);
        $data = json_decode($res, true);
        $list = $data['feedList'];
        if (count($list)) {
            foreach ($list as $item) {
                if (!array_key_exists('post_id', $item) || !array_key_exists('images', $item) || !count($item['images'])) {
                    continue;
                }
                $info['post_id'] = $item['post_id'];
                $info['type'] = array_key_exists('type', $item) ? $item['type'] : '';
                $info['passed_time'] = array_key_exists('passed_time', $item) ? $item['passed_time'] : '';
                $info['content'] = array_key_exists('content', $item) ? $item['content'] : '';
                $info['title'] = array_key_exists('title', $item) ? $item['title'] : '';
                if (!array_key_exists('img_id', $item['images'][0])) {
                    continue;
                }
                $info['img_id'] = array_key_exists('img_id', $item['images'][0]) ? $item['images'][0]['img_id'] : '';
                $info['user_id'] = array_key_exists('user_id', $item['images'][0]) ? $item['images'][0]['user_id'] : '';
                $info['width'] = array_key_exists('width', $item['images'][0]) ? $item['images'][0]['width'] : '';
                $info['height'] = array_key_exists('height', $item['images'][0]) ? $item['images'][0]['height'] : '';
                $info['author'] = array_key_exists('name', $item) ? $item['site']['name'] : '';
                $info['createtime'] = time();
                $info['updatetime'] = time();
                Db::name('meiri_tuchong')->insert($info, true);
            }
            $end = end($list);
            return $end['post_id'];
        }

        return false;
    }

    // 获取图片组合
    public function get_img_list($page = 1)
    {
        $util = new Utils();
        $tu_list = $util->get_rand(15, 'meiri_tuchong');
        $rand = $this->get_rand_list();

        $list[1] = array();
        $list[2] = array();
        $list[3] = array();
        foreach ($tu_list as $tu) {
            $tu['img_src'] = 'https://photo.tuchong.com/' . $tu['user_id'] . '/f/' . $tu['img_id'] . '.jpg';
            if (count($list[1]) < $rand[0]) {
                array_push($list[1], $tu);
            } elseif (count($list[2]) < $rand[1]) {
                array_push($list[2], $tu);
            } else {
                array_push($list[3], $tu);
            }
        }
        $data = array();
        foreach ($list as $item) {
            if ($item = $this->handler_img_list($item)) {
                array_push($data, $item);
            }
        }

        $this->result('list', $data);
    }

    // 分配图片组合
    public function handler_img_list($list)
    {
        switch (count($list)) {
            case 6:
                return array(array_slice($list, 0, 3), array_slice($list, 3));
            case 3:
            case 4:
            case 7:
            case 9:
                return array(array_slice($list, 0, 1), array_slice($list, 1));
            case 5:
            case 8:
            case 10:
                return array(array_slice($list, 0, 2), array_slice($list, 2));
            default:
                return false;
        }
    }

    // 随机3个随机数数组
    public function get_rand_list($base_count = 15)
    {
        $count3 = 0;
        $count1 = mt_rand(5, 10);
        $next1 = $base_count - $count1;
        $count2 = mt_rand(3, $next1);
        $next2 = $next1 - $count2;
        if ($next2 < 3) {
            $count2 = $next1;
        } else {
            $count3 = $next2;
        }
        return [$count1, $count2, $count3];
    }
}
